var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var that, app = getApp();
let interstitialAd = null;

function compareV(t, a) {
    if (t && a) {
        for (var e = t.split("."), o = a.split("."), n = Math.min(e.length, o.length), i = 0, s = 0; i < n && 0 == (s = parseInt(e[i]) - parseInt(o[i])); ) i++;
        return 0 <= (s = 0 != s ? s : e.length - o.length);
    }
    return console.log("版本号不能为空"), !1;
}

Page({
    data: {
        is_request: 1,
        setTimeself: "",
        time: 0,
        loading: !1,
        suspension_style: [ "left:90rpx;top:80rpx", "right:100rpx;top:80rpx", "left:120rpx;bottom:60rpx", "right:170rpx;bottom:60rpx", "right:330rpx;top:30rpx", "right:75rpx;bottom:70rpx" ],
        videoAd: "",
        request: 1,
        show_single: !0
    },
    adload: function(t) {
        console.log(t);
    },
    aderror1: function(t) {
        console.log("原生广告加载失败"), this.setData({
            show_single: !1
        });
    },
    a: function() {},
    onLoad: function(t) {
        t.parent_id && wx.setStorageSync("parent_id", t.parent_id), that = this, compareV(wx.getSystemInfoSync().SDKVersion, "2.10.4") || (console.log(2121), 
        this.setData({
            show_single: !1
        }));
    },
    onClickAddToMinProgramCloseBtn: function() {
        wx.setStorageSync("showAddMeFlag", !0), this.setData({
            showAddMeBtn: !1
        });
    },
    onReady: function() {
        wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#000000"
        }), wx.getStorageSync("showAddMeFlag") || that.setData({
            showAddMeBtn: !0
        }), wx.createSelectorQuery().select("#global-nav").boundingClientRect(function(t) {
            that.setData({
                m_height: t.height
            });
        }).exec();
        var t = {
            action: "home",
            contr: "index",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info), that.clearTime(), that.countdown(), that.loadSuspension();
            var a = wx.getSystemInfoSync();
            t.info.config.video_id && compareV(a.SDKVersion, "2.6.0") && (that.data.videoAd = wx.createRewardedVideoAd({
                adUnitId: t.info.config.video_id
            }), that.data.videoAd.onError(function(t) {
                console.log(t);
            })), wx.createInterstitialAd && that.data.config.screen_id && (that.data.interstitialAd = wx.createInterstitialAd({
                adUnitId: that.data.config.screen_id
            }), that.data.interstitialAd.onError(function(t) {
                console.log(t);
            }), that.data.interstitialAd.show().catch(function(t) {
                console.error(t);
            }));
        });
    },
    onShow: function() {
        var t = wx.getStorageSync("task");
        t && (Date.parse(new Date()) - t.time > 1e3 * parseInt(t.demo_time) ? that.receive(t.id) : wx.showModal({
            title: "温馨提示",
            content: "需要试玩" + t.demo_time + "秒才能获得奖励哦",
            showCancel: !1,
            success: function(t) {}
        }), wx.removeStorageSync("task"));
        var a = wx.getUpdateManager();
        a && a.onCheckForUpdate && (a.onCheckForUpdate(function(t) {
            console.log(t.hasUpdate);
        }), a.onUpdateReady(function() {
            wx.showModal({
                title: "更新提示",
                content: "新版本已经准备好，是否重启应用？",
                success: function(t) {
                    t.confirm && a.applyUpdate();
                }
            });
        }), a.onUpdateFailed(function() {
            wx.showModal({
                title: "更新提示",
                content: "新版本下载失败",
                showCancel: !1
            });
        }));
        // 在页面onShow回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
    interstitialAd = wx.createInterstitialAd({
      adUnitId: 'adunit-42e141bfcfdcc315'
    })
    interstitialAd.onLoad(() => {})
    interstitialAd.onError((err) => {})
    interstitialAd.onClose(() => {})
  };
  
  // 在适合的场景显示插屏广告
  if (interstitialAd) {
    interstitialAd.show().catch((err) => {
      console.error(err)
    })
  };
    },
    loadSuspension: function() {
        var t = {
            action: "suspension",
            contr: "index",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        });
    },
    clickSuspension: function(t) {
        var e = t.currentTarget.dataset.index;
        1 == this.data.task[e].type ? wx.navigateTo({
            url: "/bh_bargain/pages/luck/luck"
        }) : 2 == this.data.task[e].type ? this.videoAdShow(this.data.task[e].id) : 3 == this.data.task[e].type && wx.navigateToMiniProgram({
            appId: that.data.task[e].appid,
            path: that.data.task[e].path,
            extraData: {},
            envVersion: "release",
            success: function(t) {
                var a = {
                    id: that.data.task[e].id,
                    demo_time: that.data.task[e].number,
                    time: Date.parse(new Date())
                };
                wx.setStorageSync("task", a);
            },
            fail: function(t) {}
        });
    },
    videoAdShow: function(a) {
        this.data.videoAd && (this.data.videoAd.show().catch(function() {
            that.data.videoAd.load().then(function() {
                return that.data.videoAd.show();
            }).catch(function(t) {
                wx.showToast({
                    icon: "none",
                    title: "激励视频加载失败~"
                });
            });
        }), that.data.videoAd.offError(function(t) {}), that.data.videoAd.onClose(function(t) {
            that.data.videoAd.offClose(), (t && t.isEnded || void 0 === t) && that.receive(a);
        }));
    },
    receive: function(t) {
        var a = {
            action: "receive",
            contr: "sign",
            id: t,
            token: wx.getStorageSync("token")
        };
        1 == this.data.is_request && (this.data.is_request = 2, _tools2.default.request(a, function(t) {
            that.loadSuspension(), that.data.is_request = 1, wx.showToast({
                title: "领取奖励成功"
            });
        }));
    },
    gotoBargain: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/bh_bargain/pages/bargain/bargain?id=" + a
        });
    },
    gotoGoods: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/bh_bargain/pages/goods/goods?id=" + a
        });
    },
    dargaining: function(t) {
        var a = t.currentTarget.dataset.id;
        if (1 == this.data.request) {
            this.data.request = 2;
            var e = {
                action: "launch",
                contr: "help",
                token: wx.getStorageSync("token"),
                goods_id: a
            };
            _tools2.default.request(e, function(t) {
                that.data.request = 1, 2 == t.status ? wx.showModal({
                    title: "提示",
                    mask: !0,
                    content: t.info,
                    showCancel: !1
                }) : wx.reLaunch({
                    url: "/bh_bargain/pages/bargain/bargain?id=" + t.info.id
                });
            });
        }
    },
    getUserInfo: function(a) {
        if ("getUserInfo:ok" == a.detail.errMsg) {
            var t = {
                action: "register",
                contr: "my",
                token: wx.getStorageSync("token"),
                encryptedData: a.detail.encryptedData,
                iv: a.detail.iv
            };
            _tools2.default.request(t, function(t) {
                that.dargaining(a);
            });
        }
    },
    gotoList: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/goodsList/goodsList"
        });
    },

    gotoJf: function() {
                wx.navigateTo({
                    url: "/bh_bargain/pages/goodsconvert/goodsconvert"
                });
            },
    gotoChoujiang: function() {
                wx.navigateTo({
                    url: "/bh_bargain/pages/luck/luck"
                });
            },

    clearTime: function() {
        clearTimeout(that.data.setTimeself);
    },
    onUnload: function() {
        this.clearTime();
    },
    countdown: function() {
        if (null != this.data.bargain_goods && this.data.bargain_goods) {
            for (var t in this.data.time++, this.data.bargain_goods) {
                var a = parseInt(that.data.bargain_goods[t].surplus_time) - this.data.time;
                if (that.data.bargain_goods[t].d = "00", that.data.bargain_goods[t].h = "00", that.data.bargain_goods[t].m = "00", 
                that.data.bargain_goods[t].s = "00", 0 < a) {
                    var e = parseInt(a / 60 / 60 / 24, 10), o = Math.floor(a / 60 / 60 % 24), n = Math.floor(a / 60 % 60), i = Math.floor(a % 60);
                    o < 10 && (o = "0" + o), e < 10 && (e = "0" + e), n < 10 && (n = "0" + n), i < 10 && (i = "0" + i), 
                    that.data.bargain_goods[t].d = e, that.data.bargain_goods[t].h = o, that.data.bargain_goods[t].m = n, 
                    that.data.bargain_goods[t].s = i;
                }
            }
            that.data.setTimeself = setTimeout(function() {
                that.setData({
                    bargain_goods: that.data.bargain_goods
                }), that.countdown();
            }, 1e3);
        } else clearTimeout(that.data.setTimeself);
    },
    onShareAppMessage: function() {
        return {
            title: this.data.share.text,
            imageUrl: this.data.share.images,
            path: "bh_bargain/pages/index/index?parent_id=" + this.data.share.member_id
        };
    }
});